//
//  DWUserDefaults.m
//  Unicorn
//
//  Created by Marshall Yang on 2018/1/8.
//  Copyright © 2018年 Marshall Yang. All rights reserved.
//

#import "BWUserDefaults.h"

#define UserDefaults [NSUserDefaults standardUserDefaults]

@implementation BWUserDefaults

#pragma mark - 用户相关
/// 保存用户信息
+ (void)saveUserInfo:(NSDictionary *)userInfo {
    [self saveUserInfoWithUserDict:userInfo];
}

/// 根据model保存用户信息
+ (void)saveUserInfoWithModel:(BWUserInfoModel *)userModel {
    NSDictionary *userInfo = [userModel mj_keyValues];
    [self saveUserInfoWithUserDict:userInfo];
}

+ (void)saveUserInfoWithUserDict:(NSDictionary *)dict {
    if (dict == nil) {
        // 退出登录
        [self saveUserLoginState:NO];
    } else {
        // 登录
        [self saveUserLoginState:YES];
    }
    // 保存用户信息
    NSMutableDictionary *resultDict = [self removeDictionaryNullValue:dict];
    [UserDefaults setObject:resultDict forKey:UserInfo];
    [UserDefaults synchronize];
}

/// 剔除字典里的空值
+ (NSMutableDictionary *)removeDictionaryNullValue:(NSDictionary *)dict {
    NSMutableDictionary *userDict = [NSMutableDictionary dictionaryWithDictionary:dict];
    for (NSString *key in [userDict allKeys]) {
        id value = userDict[key];
        if ([value isKindOfClass:[NSArray class]]) {
            NSMutableArray *childArray = [NSMutableArray arrayWithArray:value];
            if (childArray.count == 0) {
                [userDict removeObjectForKey:key];
            } else {
                NSMutableArray *tempArray = [self removeArrayNullValue:childArray];
                [userDict setObject:tempArray forKey:key];
            }
        }
        else if ([value isKindOfClass:[NSDictionary class]]) {
            NSMutableDictionary *childDict = [NSMutableDictionary dictionaryWithDictionary:value];
            if ([childDict allKeys].count == 0) {
                [userDict removeObjectForKey:key];
            } else {
                NSMutableDictionary *newDict = [self removeDictionaryNullValue:childDict];
                [userDict setObject:newDict forKey:key];
            }
            
        }
        else if (!value || value == nil || [value isKindOfClass:[NSNull class]]) {
            [userDict removeObjectForKey:key];
        }
    }
    NSLog(@"剔除字典里的空值 -- %@", userDict);
    return userDict;
}

/// 剔除数组里的空值
+ (NSMutableArray *)removeArrayNullValue:(NSArray *)array {
    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:array];
    for (int i = 0; i < tempArray.count; i++) {
        id child = tempArray[i];
        if ([child isKindOfClass:[NSArray class]]) {
            NSMutableArray *childArray = [self removeArrayNullValue:child];
            [tempArray replaceObjectAtIndex:i withObject:childArray];
        } else if ([child isKindOfClass:[NSDictionary class]]) {
            NSMutableDictionary *childDict = [self removeDictionaryNullValue:child];
            [tempArray replaceObjectAtIndex:i withObject:childDict];
        } else if (!child || child  == nil || [child isKindOfClass:[NSNull class]]) {
            [tempArray removeObject:child];
        }
    }
    return tempArray;
}

/// 获取用户信息
+ (BWUserInfoModel *)getUserInfo {
    if ([UserDefaults objectForKey:UserInfo] != nil) {
        NSDictionary *dict = [UserDefaults objectForKey:UserInfo];
        return [BWUserInfoModel mj_objectWithKeyValues:dict];
    }
    return nil;
}

/// 登录状态
+ (BOOL)userLoginState {
    if ([UserDefaults objectForKey:UserLoginState] == nil) {
        [UserDefaults setObject:@(NO) forKey:UserLoginState];
        [UserDefaults synchronize];
        return NO;
    }
    BOOL state = [[UserDefaults objectForKey:UserLoginState] boolValue];
    return state;
}
+ (void)saveUserLoginState:(BOOL)state {
    [UserDefaults setObject:@(state) forKey:UserLoginState];
    
    // 退出登录
    if (!state) {
        // 清空用户信息
        [UserDefaults setObject:nil forKey:UserID];
        [UserDefaults setObject:nil forKey:UserToken];
        [UserDefaults setObject:nil forKey:UserPhoneNumber];
        [UserDefaults setObject:nil forKey:UserPassword];
    }
    
    [UserDefaults synchronize];
}

/// token
+ (NSString *)userToken {
    BWUserInfoModel *model = [self getUserInfo];
    return kCheckString(model.token);
}
+ (void)saveUserToken:(NSString *)token {
    BWUserInfoModel *model = [self getUserInfo];
    if (model == nil) {
        model = [[BWUserInfoModel alloc] init];
    }
    model.token = token;
    [self saveUserInfoWithModel:model];
}

/// uid
+ (NSString *)userId {
    BWUserInfoModel *model = [self getUserInfo];
    return kCheckString(model.userId);
}
+ (void)saveUserId:(NSString *)userId {
    BWUserInfoModel *model = [self getUserInfo];
    if (model == nil) {
        model = [[BWUserInfoModel alloc] init];
    }
    model.userId = userId;
    [self saveUserInfoWithModel:model];
}

/// 用户名
+ (NSString *)userName {
    BWUserInfoModel *model = [self getUserInfo];
    return kCheckString(model.username);
}
+ (void)saveUserName:(NSString *)userName {
    BWUserInfoModel *model = [self getUserInfo];
    if (model == nil) {
        model = [[BWUserInfoModel alloc] init];
    }
    model.username = userName;
    [self saveUserInfoWithModel:model];
}

/// 昵称
+ (NSString *)nickname {
    BWUserInfoModel *model = [self getUserInfo];
    return kCheckString(model.nickname);
}
+ (void)saveNickname:(NSString *)nickname {
    BWUserInfoModel *model = [self getUserInfo];
    if (model == nil) {
        model = [[BWUserInfoModel alloc] init];
    }
    model.nickname = nickname;
    [self saveUserInfoWithModel:model];
}

/// 手机号
+ (NSString *)userPhoneNumber {
    BWUserInfoModel *model = [self getUserInfo];
    return kCheckString(model.mobile);
}
+ (void)saveUserPhoneNumber:(NSString *)phoneNumber {
    BWUserInfoModel *model = [self getUserInfo];
    if (model == nil) {
        model = [[BWUserInfoModel alloc] init];
    }
    model.mobile = phoneNumber;
    [self saveUserInfoWithModel:model];
}

/// 头像
+ (NSString *)userHeadImage {
    BWUserInfoModel *model = [self getUserInfo];
    return kCheckString(model.avatar);
}
+ (void)saveUserHeadImage:(NSString *)headImage {
    BWUserInfoModel *model = [self getUserInfo];
    if (model == nil) {
        model = [[BWUserInfoModel alloc] init];
    }
    model.avatar = headImage;
    [self saveUserInfoWithModel:model];
}

/// 用户所在城市
+ (NSString *)userCityName {
    NSString *cityName = [UserDefaults objectForKey:UserCityName];
    return kCheckString(cityName);
}
+ (void)saveUserCityName:(NSString *)city {
    [UserDefaults setObject:kCheckString(city) forKey:UserCityName];
    [UserDefaults synchronize];
}

/// 用户当前经纬度
+ (CLLocationCoordinate2D)userLocation {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dict = [userDefaults objectForKey:UserLocation];
    double lat = [dict[@"lat"] doubleValue];
    double lng = [dict[@"lng"] doubleValue];
    return CLLocationCoordinate2DMake(lat, lng);
}
+ (void)saveUserLocation:(CLLocationCoordinate2D)location {
    NSDictionary *dict = @{
        @"lat" : @(location.latitude),
        @"lng" : @(location.longitude),
    };
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:dict forKey:UserLocation];
    [userDefaults synchronize];
}


#pragma mark - 司机相关

@end
